package yancy.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import yancy.common.utils.PageUtils;
import yancy.modules.sys.dao.SysBorrowBookDao;
import yancy.modules.sys.entity.Pair;
import yancy.modules.sys.entity.SysBook;
import yancy.modules.sys.entity.SysBorrowBook;
import yancy.modules.sys.entity.SysFile;
import yancy.modules.sys.service.SysBookAuthorService;
import yancy.modules.sys.service.SysBookService;
import yancy.modules.sys.service.SysBorrowBookService;
import yancy.modules.sys.service.SysFileService;
import lombok.AllArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class SysBorrowBookServiceImpl extends ServiceImpl<SysBorrowBookDao, SysBorrowBook>
    implements SysBorrowBookService {


    private final SysBookAuthorService bookAuthorService;

    private final SysBookService bookService;

    private final SysFileService fileService;


    @Override
    public Page query(Page page, Map<String, Object> params) {
        List<SysBorrowBook> bbList = baseMapper.query(page, params);

        List<Integer> fileIds = bbList.stream()
            .map(SysBorrowBook::getBook)
            .map(SysBook::getFileIds)
            .filter(StringUtils::isNumeric)
            .map(Integer::valueOf)
            .collect(Collectors.toList());

        List<SysFile> files = fileService.list(new QueryWrapper<SysFile>().in(CollectionUtils.isNotEmpty(fileIds), "file_id", fileIds));
        if (CollectionUtils.isNotEmpty(files)) {
            for (SysBorrowBook bb : bbList) {
                if (!StringUtils.isNumeric(bb.getBook().getFileIds()))
                    continue;
                for (SysFile file : files) {
                    if (Integer.parseInt(bb.getBook().getFileIds()) == file.getFileId()) {
                        bb.getBook().setFileList(Collections.singletonList(file));
                        break;
                    }
                }
            }
        }

        return page.setRecords(bbList);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
//        String username = (String)params.get("name");
//        Long createUserId = (Long)params.get("createUserId");
//
//        IPage<SysBook> page = this.page(
//                new Query<SysBook>().getPage(params),
//                new QueryWrapper<SysBook>()
//                        .like(StringUtils.isNotBlank(username),"name", username)
//                        .eq(createUserId != null,"create_user_id", createUserId)
//        );

//        return new PageUtils(page);
        return null;
    }

    @Override
    @Transactional
    public void saveBook(SysBook book) {

    }


    @Override
    @Transactional
    public void update(SysBook book) {

    }


    @Override
    public PageUtils myBooks(Map<String, Object> params) {
        return null;
    }

    @Override
    public List<Pair> queryCountByTime(String startTime, String endTime) {
        return baseMapper.queryCountByTime(startTime, endTime);
    }

    @Override
    @Transactional
    public void borrow(SysBorrowBook borrowBook) {
        SysBook book = bookService.getById(borrowBook.getBookId());
        if (book.getAvailable() < borrowBook.getTotal()) {
            throw new IllegalArgumentException("当前图书库存不足");
        }
        book.decAvail(borrowBook.getTotal());
        bookService.update(book);

        this.save(borrowBook);
    }

    private void updateAvailable(SysBook b) {
    }


    private void checkAuthor(SysBook book) {


    }
}




